﻿<%@ WebHandler Language="C#" Class="KPIReport.Web.AjaxHandler.Administration.Catalog.TransaksiHdrHandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

using KPIReport.Library.DataAccess.Administration;
using KPIReport.Library.DomainObject.Administration;
using KPIReport.Library.DataAccess.Administration.UserManagement;
using KPIReport.Library.DomainObject.Administration.UserManagement;

using KPIReport.Library.DataAccess.Administration.Catalog;
using KPIReport.Library.DomainObject.Administration.Catalog;

using KPIReport.Library.Enum;
using KPIReport.Web.Support;

using Newtonsoft.Json.Linq;

namespace KPIReport.Web.AjaxHandler.Administration.Catalog
{
    public class TransaksiHdrHandler : KPIReport.Web.BaseHandler
    {

        private transaksihdrRepo transaksihdrDal = new transaksihdrRepo(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);
        private recLogRepo logDal = new recLogRepo(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);
        private string MethodName = string.Empty;


        public override void ProcessRequest(HttpContext context)
        {

            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];

            context.Response.Clear();
            context.Response.Buffer = true;

            System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);

            string jsonString = string.Empty;
            jsonString = context.Server.UrlDecode(sr.ReadToEnd());
            JObject jObj = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);

            sr.Close();
            sr.Dispose();

            switch (MethodName.ToLower())
            {
                
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;
                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        

        private string GetData(HttpContext context)
        {
            JSONHelper helper = new JSONHelper();
            //string Isinternal = context.Request.Params["UserID"];
            string parameters = Helper.JqGridFilterToSQLParams(context);
            
            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? recRole.Fields.RoleName : context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());

            int totalCount = 0;
            int totalPages = 0;
            List<transaksihdr> pagedList = null;

            try
            {
                totalCount = Convert.ToInt32(transaksihdrDal.SelectScalar(SQL.Function.Aggregate.Count,"[Salesman Name]", parameters));

                decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
                totalPages = Convert.ToInt32(dc);

                pagedList = transaksihdrDal.GetList(pageSize, page, sidx, sord, parameters);
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "GetData";
                log.LogMessage = helper.Message;
                log.LogSource = "sRolehandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
                log.Dispose();
            }

            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = pagedList,
            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);

        }

    }

}